package com.atguigu.auth.process;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.activiti.engine.task.Task;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;

/**
 * @Author Yang
 * @Date 2023/10/25 15:32
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProcessTestGateway {


    // 流程定义信息
    @Autowired
    private RepositoryService repositoryService;

    // 流程实例
    @Autowired
    private RuntimeService runtimeService;

    // 任务
    @Autowired
    private TaskService taskService;

    // 历史相关
    @Autowired
    private HistoryService historyService;


    // 1 部署流程定义
    @Test
    public void deployProcess() {
        // 流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia002.bpmn20.xml")
                .name("请假申请流程002")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    // 2 启动流程
    @Test
    public void startUpProcess(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("day","3");
        //创建流程实例,我们需要知道流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia002",map);
        //输出实例的相关信息
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id：" + processInstance.getId());
        System.out.println("当前活动Id：" + processInstance.getActivityId());
    }

    // 3 查询任务
    @Test
    public void finfTaskList(){
        String assign = "gousheng";
        // 查询张三的任务
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(assign).list();
        for (Task task : taskList) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }

    // 4 执行任务
    @Test
    public void completTask(){
        Task task = taskService.createTaskQuery()
                .taskAssignee("xiaocui")  //要查询的负责人
                .singleResult();//返回一条

        //完成任务,参数：任务id
        taskService.complete(task.getId());
    }


    /**
     * 查询已处理历史任务
     */
    @Test
    public void findProcessedTaskList() {
        //张三已处理过的历史任务
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee("zhangsan2").finished().list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("流程实例id：" + historicTaskInstance.getProcessInstanceId());
            System.out.println("任务id：" + historicTaskInstance.getId());
            System.out.println("任务负责人：" + historicTaskInstance.getAssignee());
            System.out.println("任务名称：" + historicTaskInstance.getName());
        }
    }


    // 查询个人待办理的业务
    @Test
    public void findTaskList(){
        String assign = "xiaocui";
        // 查询张三的任务
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(assign).list();
        for (Task task : taskList) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }



}
